<?php
/**
 * Created by PhpStorm.
 * User: xiangfeng
 * Date: 2022/4/10
 * Time: 1:50 PM
 */

namespace app\api\controller;

use app\api\http\exceptions\ApiErr;
use app\api\http\exceptions\ApiExceptions;
use app\api\service\UserFollowService;
use app\api\service\UserService;

/**
 *
 * Class Friend
 * @package app\api\controller
 */
class Friend extends BaseApi
{
    /**
     * 1、查询好友申请列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getFriendApplyList()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $page = isset($param['cur_page']) ? (int)$param['cur_page'] : 1;
        $limit = isset($param['cur_size']) ? (int)$param['cur_size'] : 20;
        $page_config = [
            "list_rows" => $limit,
            "page" => $page
        ];
        $data = UserFollowService::getFriendApplyList($page_config, $user_id);
        return $this->jsonSuccessResponse($data);
    }

    /**
     * 2、加好友（关注）
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userJoinFriend()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $focus_user_id = isset($param['focus_user_id']) ? $param['focus_user_id'] : 0;
        if ($focus_user_id <= 0 || $user_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $data = [
            'user_id' => $user_id,
            'focus_user_id' => $focus_user_id,//被加好友ID
        ];
        $result = UserFollowService::addUserFollow($data);
        if ($result) {
            $redis = $this->redis;
            $key = USER_FARIAEND_JOIN_LIST . '_' . date('Ymd', time());
            $data['join_time'] = date('Y-m-d H:i:s', time());
            $redis->lPush($key, json_encode($data));
        }
        return $this->jsonSuccessResponse($result);
    }

    /**
     * 3、好友申请处理（取消关注&同意关注）
     * @return \think\response\Json
     * @throws ApiExceptions
     */
    public function auditFriendApply()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $id = isset($param['id']) ? $param['id'] : 0;
        $status = isset($param['status']) ? $param['status'] : 0;
        if ($id <= 0 || $user_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $result = UserFollowService::auditFriendApply($user_id, $id, $status);
        if ($result) {
            $redis = $this->redis;
            $key = USER_FARIAEND_AUDIT_LIST . '_' . date('Ymd', time());
            $data = [
                'user_id' => $user_id,
                'id' => $id,
                'status' => $status,
                'audit_time' => date('Y-m-d H:i:s', time()),
            ];
            $redis->lPush($key, json_encode($data));
        }
        return $this->jsonSuccessResponse($result);
    }

    /**
     * 查询好友详情
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFriendInfo()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $friend_user_id = isset($param['friend_user_id']) ? (int)$param['friend_user_id'] : 0;
        if ($friend_user_id <= 0 || $user_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $data = UserService::getFriendInfo($user_id, $friend_user_id);
        return $this->jsonSuccessResponse($data);
    }
}